package cn.wolfcode.luowowo.controller;

import cn.wolfcode.luowowo.annotation.RequireLogin;
import cn.wolfcode.luowowo.cache.redis.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.domain.UserInfo;
import cn.wolfcode.luowowo.service.IUserInfoService;
import cn.wolfcode.luowowo.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("users")
public class UserInfoController {


    // 配置属性是在 website-api 项目中， 使用的位置 userInfoServiceImpl 这个类
    // 如果直接将 3 个属性添加到 userInfoServiceImpl 类中， mgrsite 项目启动时会报错
    // 原因： mgrsite 没有这个三个配置
    @Value("${sms.url}")
    private String smsUrl;

    @Value("${sms.msg}")
    private String smsMsg;

    @Value("${sms.appkey}")
    private String smsAppkey;

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;


    @GetMapping("/checkPhone")
    public Object checkPhone(String phone){
        boolean ret = userInfoService.checkPhone(phone);
        return ret;
    }

    @RequireLogin // 访问这个方法必须登录才能操作
    @GetMapping("/getCurrentUser")
    public Object getCurrentUser(HttpServletRequest request){
        String token = request.getHeader("token");

        UserInfo userInfo = userInfoRedisService.getUserByToken(token);

        return JsonResult.success(userInfo);
    }

    @GetMapping("/sendVerifyCode")
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone,smsUrl,smsMsg,smsAppkey);
        return JsonResult.success();
    }

    @PostMapping("/regist")
    public Object regist(String verifyCode, String phone, String nickname, String password, String rpassword){
        /*boolean ret = userInfoService.regist(verifyCode,phone);
        if(!ret) {
            UserInfo userInfo = new UserInfo();
            userInfo.setPhone(phone);
            userInfo.setNickname(nickname);
            userInfo.setPassword(password);
            userInfoService.save(userInfo);
            return new JsonResult<String>(JsonResult.CODE_SUCCESS,"验证码正确",null);
        }
        return new JsonResult<String>(JsonResult.CODE_ERROR_PARAM,"验证码错误",null);*/

        userInfoService.regist(verifyCode, phone, nickname, password, rpassword);
        return JsonResult.success();
    }

    @PostMapping("/login")
    public Object login(String username,String password){
        // 登录
        UserInfo userInfo = userInfoService.login(username,password);
        System.out.println(userInfo);

        // 将用户缓存到redis中
        String token = userInfoRedisService.setToken(userInfo);

        // 页面需要 token user
        Map<String,Object> map = new HashMap<>();
        map.put("token",token);
        map.put("user",userInfo);
        return JsonResult.success(map);
    }
}
